<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 云淡风轻(97139915@qq.com)
// +----------------------------------------------------------------------

// 商品
class GoodsAction extends CommonAction{
	//今日列表
	public function now()
	{
		$now_time = gmtTime();
		$site_name = trim($_REQUEST['site_name']);
		$cate_id = intval($_REQUEST['cate_id']);
		$name = trim($_REQUEST['name']);
		$status = trim($_REQUEST['status']);
		$city = trim($_REQUEST['city']);

		$is_empty = false;
		$where = '';

		$parameter = array();

		if(!empty($site_name))
		{
			$this->assign("site_name",$site_name);
			$parameter['site_name'] = $site_name;
			$ids = array();
			$site_ids = D('Site')->where("name like '%$site_name%'")->field('id')->findAll();
			foreach($site_ids as $site)
			{
				$ids[] = $site['id'];
			}

			if(count($ids) > 0)
				$where .= ' and gk.site_id in ('.implode(',', $ids).')';
			else
				$is_empty = true;
		}

		if(!empty($cate_id))
		{
			$this->assign("cate_id",$cate_id);
			$where.=" and gk.cate_id = $cate_id";
			$parameter['cate_id'] = $cate_id;
		}

		if(!empty($city))
		{
			$this->assign("city",$city);
			$where.=" and gk.city = '$city'";
			$parameter['city'] = $city;
		}

		if(!empty($name))
		{
			$name_query = clearSymbol($name);
			if(!empty($name_query))
			{
				$name_query = segmentToUnicode($name_query,'+');
				$match_sql = ' inner join '.C("DB_PREFIX").'goods_match as gm ON gm.id = gk.id ';
				$where.=" AND MATCH (gm.content) AGAINST ('$name_query' IN BOOLEAN MODE)";
				$this->assign("name",$name);
				$parameter['name'] = $name;
			}
		}

		if($status != "" && $status >= 0)
		{
			$where.=" and g.status = $status";
			$parameter['status'] = $status;
			$this->assign("status",$status);
		}
		else
		{
			$this->assign("status",-1);
		}

		$model = M();

		if(!$is_empty)
		{
			$count_str = 'SELECT count(g.id) as gcount
				FROM '.C("DB_PREFIX").'goods_key as gk
				inner join '.C("DB_PREFIX").'goods as g on g.id = gk.id '.$match_sql.'
				where gk.end_time > '.$now_time.$where;

			$count = $model->query($count_str);
			$count = $count[0]['gcount'];

			$sql_str = 'SELECT g.id,g.name,g.shop_price,g.city,g.end_time,g.status,g.is_best,g.sort,g.cate_id,g.bought,g.site_name
					FROM '.C("DB_PREFIX").'goods_key as gk
					inner join '.C("DB_PREFIX").'goods as g on g.id = gk.id '.$match_sql.'
					where gk.end_time > '.$now_time.$where.' ';

			$voList = $this->_sqlList($model, $sql_str,$count, $parameter, 'id', false);
		}
		else
			$voList = array();

		L("FORBID","未审核");
		L("NORMAL","已审核");

		$group_city_list = D("GroupCity")->where("status=1")->order("sort asc,id asc")->findAll();
		$this->assign("group_city_list",$group_city_list);

		$goodsCates = D('GoodsCate')-> where("status=1")-> findAll();
		$goodsCates = D('GoodsCate')-> toFormatTree($goodsCates,array('name'));
		$this->assign("goodsCates",$goodsCates);

		$this->display();
	}

	//往期团购列表
	public function expired()
	{
		$partitions = D("Goods")->getGoodsPartitions();
		$this->assign("partitions",$partitions);

		$site_name = trim($_REQUEST['site_name']);
		$cate_id = intval($_REQUEST['cate_id']);
		$name = trim($_REQUEST['name']);
		$status = trim($_REQUEST['status']);
		$city = trim($_REQUEST['city']);
		$end_time = intval($_REQUEST['end_time']);
		if(isset($partitions[$end_time]))
			$partition = $partitions[$end_time];
		else
		{
			$partition = current($partitions);
			$end_time = $partition['min_date'];
		}
		$this->assign("end_time",$end_time);

		$parameter = array();
		$parameter['end_time'] = $end_time;

		$is_empty = false;
		$where = '';

		if(!empty($site_name))
		{
			$this->assign("site_name",$site_name);
			$parameter['site_name'] = $site_name;
			$ids = array();
			$site_ids = D('Site')->where("name like '%$site_name%'")->field('id')->findAll();
			foreach($site_ids as $site)
			{
				$ids[] = $site['id'];
			}

			if(count($ids) > 0)
				$where .= ' and gk.site_id in ('.implode(',', $ids).')';
			else
				$is_empty = true;
		}

		if(!empty($cate_id))
		{
			$this->assign("cate_id",$cate_id);
			$where.=" and gk.cate_id = $cate_id";
			$parameter['cate_id'] = $cate_id;
		}

		if(!empty($city))
		{
			$this->assign("city",$city);
			$where.=" and gk.city = '$city'";
			$parameter['city'] = $city;
		}

		if(!empty($name))
		{
			$name_query = clearSymbol($name);
			if(!empty($name_query))
			{
				$name_query = segmentToUnicode($name_query,'+');
				$match_sql = ' inner join '.C("DB_PREFIX").'goods_match as gm ON gm.id = gk.id ';
				$where.=" AND MATCH (gm.content) AGAINST ('$name_query' IN BOOLEAN MODE)";
				$this->assign("name",$name);
				$parameter['name'] = $name;
			}
		}

		if($status != "" && $status >= 0)
		{
			$where.=" and g.status = $status";
			$this->assign("status",$status);
			$parameter['status'] = $status;
		}
		else
		{
			$this->assign("status",-1);
		}

		if(!$is_empty)
		{
			$model = M();
			$count_str = 'SELECT count(g.id) as gcount
					FROM '.C("DB_PREFIX").'goods_key as gk
					inner join '.C("DB_PREFIX").'goods as g on g.id = gk.id '.$match_sql.
					"where $partition[where] $where ";

			$count = $model->query($count_str);
			$count = $count[0]['gcount'];

			$sql_str = 'SELECT g.id,g.name,g.shop_price,g.city,g.end_time,g.status,g.is_best,g.sort,g.cate_id,g.bought,g.site_name
					FROM '.C("DB_PREFIX").'goods_key as gk
					inner join '.C("DB_PREFIX").'goods as g on g.id = gk.id  '.$match_sql.
					"where $partition[where] $where ";

			$voList = $this->_sqlList($model, $sql_str, $count,$parameter, 'id', false);
		}
		else
			$voList = array();

		L("FORBID","未审核");
		L("NORMAL","已审核");

		$group_city_list = D("GroupCity")->where("status=1")->order("sort asc,id asc")->findAll();
		$this->assign("group_city_list",$group_city_list);

		$goodsCates = D('GoodsCate')-> where("status=1")-> findAll();
		$goodsCates = D('GoodsCate')-> toFormatTree($goodsCates,array('name'));
		$this->assign("goodsCates",$goodsCates);

		$this->display();
	}

	//增
	public function add()
	{
		//网站
		$site_list = D("Site")->where("status=1")->order("sort asc,id asc")->findAll();
		$this->assign("site_list",$site_list);

		$this->assign("goods_id",0);
		$this->assign("session_id",$_SESSION['verify']);

		$new_sort = D(MODULE_NAME)->where("status=1") -> max("sort") + 1;
		$this->assign('new_sort',$new_sort);

		$goodsCates = D('GoodsCate')-> where("status=1")-> findAll();
		$goodsCates = D('GoodsCate')-> toFormatTree($goodsCates,array('name'));
		$this->assign("goodsCates",$goodsCates);

		$group_city_list = D("GroupCity")->where("status=1")->order("sort asc,id asc")->findAll();
		$this->assign("group_city_list",$group_city_list);

		$this->assign("default_end_time",toDate(gmtTime()+3600*24*15,"Y-m-d H:i:s"));
		$this->display();
	}

	public function insert()
	{
		$model = D (MODULE_NAME);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}

		//保存当前数据对象
		$goods_id = $model->add();

		if($goods_id)
		{
			//图库
			$goods_info = D("Goods")->getById($goods_id);

			$defalut_gallery_id = $_REQUEST['gallery_id'];

			if($_REQUEST['goods_gallerys'])
			{
				$goods_gallery = $_REQUEST['goods_gallerys'];
				foreach($goods_gallery as $gallery_id)
				{
					$gallery_item = D("GoodsGallery")->getById($gallery_id);

					$goods_info['big_img'] = $gallery_item['big_img'];
					$goods_info['small_img'] = $gallery_item['small_img'];

					D("GoodsGallery")->delete($gallery_item);
				}
			}

			if(!empty($_REQUEST['small_img_url']))
			{
				unlink($this->getRealPath()."/".$goods_info['big_img']);
				unlink($this->getRealPath()."/".$goods_info['small_img']);
				$goods_info['big_img'] = $_REQUEST['small_img_url'];
				$goods_info['small_img'] = $_REQUEST['small_img_url'];
			}

			$goods_info['site_name'] = D('Site')->where('id = '.$goods_info['site_id'])->getField('name');

			D("Goods")->save($goods_info);
            D("GoodsNow")->add($goods_info);
			D("GoodsKey")->add($goods_info);
			D("GoodsNowKey")->add($goods_info);

			$match_content = segmentToUnicode(clearSymbol($goods_info['name'].$goods_info['site_name']));
			$match = array(
				'id'=>$goods_id,
				'content'=>$match_content
			);

			D("GoodsMatch")->add($match);
			D("GoodsNowMatch")->add($match);

			D("Suppliers")->createSuppliers($goods_id,$goods_info['end_time']);
            D("GoodsArea")->createArea($goods_id,$goods_info['city'],$goods_info['end_time']);

			$msg = L('ADD_MSG').$goods_info['name'];
			$this->saveLog(1,$goods_id,$msg);
			$this->success (L('ADD_SUCCESS')." ".$mail_msg);
		}
		else
		{
			$msg = L('ADD_MSG').$goods_info['name'];
			$this->saveLog(0,$goods_id,$msg);
			$this->error (L('ADD_FAILED'));
		}
	}

	//改
	public function edit()
	{
		$model = M (MODULE_NAME);
		$id = $_REQUEST ['id'];
		$vo = $model->getById ( $id );

		if(!parseUrl($vo['small_img']))
			$vo['small_img'] = __ROOT__.$vo['small_img'];

		if(!parseUrl($vo['big_img']))
			$vo['big_img'] = __ROOT__.$vo['big_img'];

		$this->assign ( 'vo', $vo );

		$goodsCates = D('GoodsCate')-> where("status=1")-> findAll();
		$goodsCates = D('GoodsCate')-> toFormatTree($goodsCates,array('name'));
		$this->assign("goodsCates",$goodsCates);

		$this->assign("goods_id",$id);
		$this->assign("session_id",$_SESSION['verify']);
		$site = D("Site")->where("id = $vo[site_id]")->find();
		$this->assign("site",$site);

		$this->assign("suppliers_list",D("Suppliers")->getSuppliers($id));
        $this->assign("area_list",D("GoodsArea")->getArea($id,$vo['city']));

		$group_city_list = D("GroupCity")->where("status=1")->order("sort asc,id asc")->findAll();
		$this->assign("group_city_list",$group_city_list);

		$this->display();
	}

	public function update()
	{
        $goods_id = intval($_REQUEST['id']); //商品ID

		$model = D (MODULE_NAME);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 更新数据
		$list=$model->where('id = '.$goods_id)->save ();

		if($list)
		{
			$goods_info = D("Goods")->getById($goods_id);

			//图库
			$defalut_gallery_id = $_REQUEST['gallery_id'];

			if($_REQUEST['goods_gallerys'])
			{
				$goods_gallery = $_REQUEST['goods_gallerys'];
				foreach($goods_gallery as $gallery_id)
				{
					$gallery_item = D("GoodsGallery")->getById($gallery_id);

					$goods_info['big_img'] = $gallery_item['big_img'];
					$goods_info['small_img'] = $gallery_item['small_img'];

					D("GoodsGallery")->delete($gallery_item);
				}
			}

			if(!empty($_REQUEST['small_img_url']))
			{
				unlink($this->getRealPath()."/".$goods_info['big_img']);
				unlink($this->getRealPath()."/".$goods_info['small_img']);
				$goods_info['big_img'] = $_REQUEST['small_img_url'];
				$goods_info['small_img'] = $_REQUEST['small_img_url'];
			}

			$goods_info['site_name'] = D('Site')->where('id = '.$goods_info['site_id'])->getField('name');
			D("Goods")->where('id = '.$goods_id)->save($goods_info);
            D("GoodsKey")->where('id = '.$goods_id)->save($goods_info);
			D("GoodsNow")->where('id = '.$goods_id)->save($goods_info);
            D("GoodsNowKey")->where('id = '.$goods_id)->save($goods_info);

			$match_content = segmentToUnicode(clearSymbol($goods_info['name'].$goods_info['site_name']));
			$match = array(
				'id'=>$goods_id,
				'content'=>$match_content
			);

			D("GoodsMatch")->where('id = '.$goods_id)->save($match);
			D("GoodsNowMatch")->where('id = '.$goods_id)->save($match);

			D("Suppliers")->createSuppliers($goods_id,$goods_info['end_time']);
            D("GoodsArea")->createArea($goods_id,$goods_info['city'],$goods_info['end_time']);
			$msg = L('EDIT_MSG').$goods_info['name'];
			$this->saveLog(1,$goods_id,$msg);
			$this->success (L('EDIT_SUCCESS')." ".$mail_msg);
		}
		else
		{
			$msg = L('EDIT_MSG').$goods_info['name'];
			$this->saveLog(0,$goods_id,$msg);
			$this->error (L('EDIT_FAILED'));
		}
	}

	public function delete()
	{
		//删除指定记录
		$model = M(MODULE_NAME);
		if (! empty ( $model ))
		{
			$pk = 'id';
			$id = $_REQUEST [$pk];
			$ids = explode ( ',', $id );
			$names = '';
			$images = array();
			foreach($ids as $id)
			{
				$goods = M("Goods")->where("id=".$id)->field("name,small_img,big_img")->find();
				$names .= $goods['name'].",";
				$images[] = $goods['small_img'];
				$images[] = $goods['big_img'];
			}
			if($names!='')
			{
				$names = substr($names,0,strlen($names)-1);
			}

			if (count($ids) > 0)
			{
				$condition = array ($pk => array ('in',$ids));
				if (false !== $model->where ( $condition )->delete ())
				{
					D("GoodsKey")->where($condition)->delete();
					D("GoodsNow")->where($condition)->delete();
					D("GoodsNowKey")->where($condition)->delete();
					D("GoodsMatch")->where($condition)->delete();
					D("GoodsNowMatch")->where($condition)->delete();
					D("Suppliers")->deleteSuppliers($ids);
					D("GoodsArea")->deleteArea($ids);

					foreach($images as $img)
					{
						@unlink($this->getRealPath()."/".$img);
					}
					$msg = L('DELETE_MSG').$names;
					$this->saveLog(1,0,$msg);
					$this->success ( L('DEL_SUCCESS') );
				}
				else
				{
					$msg = L('DELETE_MSG').$names;
					$this->saveLog(0,0,$msg);
					$this->error (L('DEL_FAILED'));
				}
			}
			else
			{
				$msg = L('DELETE_MSG').',ID:'.$id;
				$this->saveLog(0,0,$msg);
				$this->error ( L('INVALID_OP') );
			}
		}
	}

	public function expiredDelete()
	{
		$partitions = D("Goods")->getGoodsPartitions();
		$site_name = trim($_REQUEST['site_name']);
		$cate_id = intval($_REQUEST['cate_id']);
		$name = trim($_REQUEST['name']);
		$status = trim($_REQUEST['status']);
		$city = trim($_REQUEST['city']);
		$end_time = intval($_REQUEST['end_time']);
		if(isset($partitions[$end_time]))
			$partition = $partitions[$end_time];
		else
			$this->redirect('Goods/expired');

		$this->assign("goods_url",U('Goods/expired'));

		$this->display();
		flush();
		ob_flush();

		$where = $partition['where'];
		$parameter .= "end_time=" .$end_time."&";

		if(!empty($site_name))
		{
			$parameter .= "site_name=" . urlencode($site_name)."&";
			$ids = array();
			$site_ids = D('Site')->where("name like '%$site_name%'")->field('id')->findAll();
			foreach($site_ids as $site)
			{
				$ids[] = $site['id'];
			}

			if(count($ids) > 0)
				$where .= ' and gk.site_id in ('.implode(',', $ids).')';
		}

		if(!empty($cate_id))
		{
			$where.=" and gk.cate_id = $cate_id";
			$parameter .= "cate_id=" . $cate_id."&";
		}

		if(!empty($city))
		{
			$where.=" and gk.city = '$city'";
			$parameter .= "city=".urlencode($city)."&";

		}

		if(!empty($name))
		{
			$name_query = clearSymbol($name);
			if(!empty($name_query))
			{
				$name_query = segmentToUnicode($name_query,'+');
				$match_sql = ' inner join '.C("DB_PREFIX").'goods_match as gm ON gm.id = gk.id ';
				$where.=" AND MATCH (gm.content) AGAINST ('$name_query' IN BOOLEAN MODE)";
				$parameter .= "name=" . urlencode($name)."&";
			}
		}

		if($status != "" && $status >= 0)
		{
			$where.=" and g.status = $status";
			$parameter .= "status=" . $status ."&";
		}
		$begin = isset($_REQUEST['begin']) ? intval($_REQUEST['begin']) : 0;

		$data_num = 0;

		$sql_str = 'SELECT count(g.id) as gcount
					FROM '.C("DB_PREFIX").'goods_key as gk
					inner join '.C("DB_PREFIX").'goods as g on g.id = gk.id '.$match_sql.
					"where $where";

		$query = M()->query($sql_str);
		if($query)
			$data_num = $query[0]['gcount'];

		$is_success = 0;

		if($data_num > 0)
		{
			$limit = $data_num;
			if($limit > 100)
				$limit = 100;

			echo "<script type=\"text/javascript\">showmessage(\"正在删除数据 $begin 到 ".($begin + $limit)."\",0);</script>"."\r\n";
			flush();
			ob_flush();

			$ids = array();
			$images = array();

			$sql_str = 'SELECT g.id,g.small_img,g.big_img
					FROM '.C("DB_PREFIX").'goods_key as gk
					inner join '.C("DB_PREFIX").'goods as g on g.id = gk.id '.$match_sql.
					"where $where LIMIT 0,".$limit;
			$data_ids = M()->query($sql_str);
			foreach($data_ids as $data)
			{
				$ids[] = $data['id'];
				if(!parseUrl($data['small_img']))
					$images[] = $data['small_img'];
				if(!parseUrl($data['small_img']))
					$images[] = $data['big_img'];
			}

			$condition = array ('id' => array ('in',$ids));
			if (false !== D("GoodsKey")->where($condition)->delete())
			{
				D("Goods")->where($condition)->delete();
				D("GoodsNow")->where($condition)->delete();
				D("GoodsNowKey")->where($condition)->delete();
				D("GoodsMatch")->where($condition)->delete();
				D("GoodsNowMatch")->where($condition)->delete();
				D("Suppliers")->deleteSuppliers($ids);
				D("GoodsArea")->deleteArea($ids);
				foreach($images as $img)
				{
					unlink($this->getRealPath()."/".$img);
				}

				echo "<script type=\"text/javascript\">showmessage(\"删除数据 $begin 到 ".($begin + $limit)."成功\",1);</script>"."\r\n";
			}
			else
			{
				echo "<script type=\"text/javascript\">showmessage(\"删除数据 $begin 到 ".($begin + $limit)."失败\",-1);</script>"."\r\n";
			}

			flush();
			ob_flush();

			if($limit < 100)
				$is_success = 1;
			else
			{
				echo "<script type=\"text/javascript\">showmessage(\"".U('Goods/expiredDelete',array('begin'=>$begin + $limit)).'&'.$parameter."\",2);</script>"."\r\n";
				flush();
				ob_flush();
				exit;
			}
		}
		else
			$is_success = 1;

		if($is_success)
		{
			//$this->saveLog(1,0,"批量删除往期团购数据，删除条件为".urldecode($parameter));
			echo "<script type=\"text/javascript\">showmessage(\"删除成功\",3);</script>"."\r\n";
			flush();
			ob_flush();
		}
		exit;
	}

	//永久删除
	public function foreverdelete()
	{
		//删除指定记录
		$model = D (MODULE_NAME);
		if (! empty ( $model ))
		{
			$pk = 'id';
			$id = $_REQUEST [$pk];
			$ids = explode ( ',', $id );
			$names = '';
			$images = array();
			foreach($ids as $id)
			{
				$goods = M("Goods")->where("id=".$id)->field("name,small_img,big_img")->find();
				$names .= $goods['name'].",";
				$images[] = $goods['small_img'];
				$images[] = $goods['big_img'];
			}

			if($names!='')
			{
				$names = substr($names,0,strlen($names)-1);
			}
			if (count($ids) > 0)
			{
				$condition = array ($pk => array ('in',$ids));
				if (false !== $model->where ( $condition )->delete ())
				{
					D("GoodsKey")->where($condition)->delete();
					D("GoodsNow")->where($condition)->delete();
					D("GoodsNowKey")->where($condition)->delete();
					D("GoodsMatch")->where($condition)->delete();
					D("GoodsNowMatch")->where($condition)->delete();
					D("Suppliers")->deleteSuppliers($ids);
					D("GoodsArea")->deleteArea($ids);

					foreach($images as $img)
					{
						unlink($this->getRealPath()."/".$img);
					}
					$msg = L('DELETE_MSG').$names;
					$this->saveLog(1,0,$msg);
					$this->success ( L('DEL_SUCCESS') );
				}
				else
				{
					$msg = L('DELETE_MSG').$names;
					$this->saveLog(0,0,$msg);
					$this->error (L('DEL_FAILED'));
				}
			}
			else
			{
				$this->saveLog(0);
				$this->error ( L('INVALID_OP') );
			}
		}
		$this->forward ();
	}

	//获取图片信息
	public function setGallery()
	{
		$id = $_REQUEST['id'];
		$gallery = D("GoodsGallery")->getById($id);
		echo json_encode($gallery);
	}

	//删除图片
	public function delGoods()
	{
		$id = $_REQUEST['id'];
		$item = D("Goods")->getById($id);
		@unlink($this->getRealPath().$item['big_img']);
		@unlink($this->getRealPath().$item['small_img']);
		D("Goods")->where('id='.$id)->setField(array('big_img','small_img'),array('',''));
		M("GoodsNow")->where('id='.$id)->setField(array('big_img','small_img'),array('',''));
	}

	//删除图片
	public function delGallery()
	{
		$id = $_REQUEST['id'];
		$item = D("GoodsGallery")->getById($id);
		@unlink($this->getRealPath().$item['origin_img']);
		@unlink($this->getRealPath().$item['big_img']);
		@unlink($this->getRealPath().$item['small_img']);
		D("GoodsGallery")->where('id='.$id)->delete();
	}

	//修改商品所属分类
	public function updateGoodsCate()
	{
		$id = intval($_REQUEST['id']);
		$cate_id = intval($_REQUEST['cate_id']);
		if(D("Goods")->where('id='.$id)->setField("cate_id",$cate_id))
		{
			D("GoodsKey")->where('id='.$id)->setField("cate_id",$cate_id);
			D("GoodsNow")->where('id='.$id)->setField("cate_id",$cate_id);
			D("GoodsNowKey")->where('id='.$id)->setField("cate_id",$cate_id);
			echo 1;
		}
		else
			echo 0;
	}

	//修改商品排序
	public function updateGoodsSort()
	{
		$id = intval($_REQUEST['id']);
		$sort = intval($_REQUEST['sort']);
		if(D("Goods")->where('id='.$id)->setField("sort",$sort))
		{
			M("GoodsKey")->where('id='.$id)->setField("sort",$sort);
			M("GoodsNow")->where('id='.$id)->setField("sort",$sort);
			M("GoodsNowKey")->where('id='.$id)->setField("sort",$sort);
			echo 1;
		}
		else
			echo 0;
	}

	//修改商品审核状态
	public function updateGoodsStauts()
	{
		$id = intval($_REQUEST['id']);
		$status = D("Goods")->where('id='.$id)->getField("status");

		if($status == 0)
			$status = 1;
		else
			$status = 0;

		if(D("Goods")->where('id='.$id)->setField("status",$status))
		{
			if($status == 0)
			{
				M("GoodsNow")->where('id='.$id)->delete();
			}
			echo $status;
		}
		else
			echo "err";
	}

	//修改商品推荐状态
	public function updateGoodsBest()
	{
		$id = intval($_REQUEST['id']);
		$is_best = D("Goods")->where('id='.$id)->getField("is_best");

		if($is_best == 0)
			$is_best = 1;
		else
			$is_best = 0;

		if(D("Goods")->where('id='.$id)->setField("is_best",$is_best))
		{
			M("GoodsNow")->where('id='.$id)->setField("is_best",$is_best);
			echo $is_best;
		}
		else
			echo "err";
	}

	public function getGoodsList()
	{
		$key = trim($_REQUEST['key']);

		if(!empty($key))
		{
			$name_query = clearSymbol($key);
			if(!empty($name_query))
			{
				$name_query = segmentToUnicode($name_query,'+');
				$match_sql = ' inner join '.C("DB_PREFIX").'goods_match as gm ON gm.id = g.id ';
				$where.=" WHERE MATCH (gm.content) AGAINST ('$name_query' IN BOOLEAN MODE)";
			}
		}

		$sql = 'SELECT g.id,g.name,g.city,g.site_name '.
					'FROM '.C("DB_PREFIX").'goods as g '.$match_sql.
					"$where order by g.id desc limit 0,30";
		$goodsList = M()->query($sql);

		echo json_encode($goodsList);
	}

	public function setting()
	{
		$this->display();
	}
}
?>